/**
 * Created by chenyong on 2015/11/10.
 * 与用户相关的服务
 * 1.添加发送验证码短信(2016-01-11)created by wangkui
 */

var express = require('express');
var request = require('request');
var router = express.Router();
var config = require('./../config');
var async = require("async");
var bigpixelsrv = require('./bigpixelsrv');
var CachemanRedis = require('cacheman-redis');
var cache = new CachemanRedis(config.cachestore);
var http = require('http');
var querystring = require('querystring');

//发送验证码短信，并将短信存储到redis数据库中，过期时间为3分钟
//采用上海微米http://www.weimi.cc/的短信平台
exports.sendVerifyCode=function(req,res){
    var notes=req.notes;
    var mobile=req.params.id;
    //生成6位随机数据
    var verifyCode="";
    for(var i=0;i<6;i++)
    {
        verifyCode+=Math.floor(Math.random()*10);
    }
    notes='[景坤科技]验证码：'+verifyCode+',3分钟内有效。如非您本人操作，可忽略本消息。';
    try{
        sendmsg(mobile,notes,function(err,data){
            var key=mobile;
            var value=verifyCode;
            cache.set(key,value,1000*3,function(error){
                if(err){console.log(error);}
            });
            bigpixelsrv.respondData(res,err,data);
        });
    }catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
};

//发送短信
var sendmsg=function(mobile,notes,callback){
    var postData = {
        uid:'Z2WytE7gMZZw',
        pas:'5jtvwtr2',
        mob:mobile,
        con:notes,
        type:'json'
    };
    var content = querystring.stringify(postData);
    var options = {
        host:'api.weimi.cc',
        path:'/2/sms/send.html',
        method:'POST',
        agent:false,
        rejectUnauthorized : false,
        headers:{
            'Content-Type' : 'application/x-www-form-urlencoded',
            'Content-Length' :content.length
        }
    };
    try{
        var req = http.request(options,function(res){
            res.setEncoding('utf8');
            res.on('data', function (chunk) {
                console.log(JSON.parse(chunk));
            });
            res.on('end',function(){
                console.log('over');
                callback('ok');
            });
        });
        req.write(content);
        req.end();
    }
    catch(e){
        callback('err');
    }
};

//校验验证码
exports.checkVerifyCode=function(req,res){
    var mobile =req.params.id;
    var verifyCode=req.verifyCode;
    var key=mobile;
    var data;
    try{
        cache.get(key,function(err,value){
            if(err|| value==undefined||value==''){
                data={
                    code:0,
                    msg:'验证码过期或不存在'
                };
            }else{
                if(value==verifyCode){
                    data={
                        code:1,
                        msg:'验证码通过'
                    };
                }else{
                    data={
                        code:0,
                        msg:'验证码不正确'
                    };
                }
            }
            bigpixelsrv.respondData(res,err,data);
        });
    }
    catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
};

//通过手机注册用户
exports.registerUser=function(req,res){

}

//用户绑定手机到用户资料中
exports.bindMobileUserinfo=function(req,res){

}
